package com.example.chuntiao.cophook.ui;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.beans.InvitInfo;
import com.example.chuntiao.cophook.mytools.HttpTool;
import com.example.chuntiao.cophook.mytools.MyApp;
import com.google.gson.Gson;

import java.io.IOException;
import java.net.ConnectException;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;


public class CreateScanCode extends AppCompatActivity {

    String oid;
    ImageView imgToRefresh;
    ImageView imgSQ;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_code);

        oid = getIntent().getStringExtra("oid");
        setTitle("请扫码");
        imgToRefresh = (ImageView) findViewById(R.id.img_to_refresh);
        progressBar = (ProgressBar) findViewById(R.id.progress_create_SQ);
        imgSQ = (ImageView) findViewById(R.id.img_SQ);
        imgToRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask<Void, Void, Bitmap>() {

                    AlertDialog alertDialog = new AlertDialog.Builder(CreateScanCode.this)
                            .setPositiveButton(R.string.yse, null)
                            .setMessage("未知错误")
                            .create();

                    @Override
                    protected void onPreExecute() {
                        imgToRefresh.setVisibility(View.INVISIBLE);
                        progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    protected Bitmap doInBackground(Void... params) {
                        String invitNum = null;
                        try {
                            invitNum = HttpTool.sendOkHttpRequest(CreateScanCode.this, HttpTool.CODE_REFRESH_INVIT_NUM, oid);


                        } catch (ConnectException e) {
                            alertDialog.setMessage("网络错误，请检查网络");
                            e.printStackTrace();
                            return null;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        if (invitNum != null && invitNum.equals("Not changed")) {
                            alertDialog.setMessage("此二维码设置为:暂不改动");
                            return null;
                        } else {
                            InvitInfo invitInfo = new InvitInfo(((MyApp) CreateScanCode.this.getApplication()).getUid(), oid, invitNum);
                            return QRCodeEncoder.syncEncodeQRCode(new Gson().toJson(invitInfo), 450);
                        }
                    }

                    @Override
                    protected void onPostExecute(Bitmap bitmap) {

                        if (bitmap == null) {
                            progressBar.setVisibility(View.INVISIBLE);
                            imgToRefresh.setVisibility(View.VISIBLE);
                            alertDialog.show();
                            return;
                        }
                        imgSQ.setImageBitmap(bitmap);
                        progressBar.setVisibility(View.INVISIBLE);
                        imgToRefresh.setVisibility(View.VISIBLE);
                        alertDialog = null;
                    }
                }.execute();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        new AsyncTask<Void, Void, Bitmap>() {
            AlertDialog alertDialog = new AlertDialog.Builder(CreateScanCode.this)
                    .setPositiveButton(R.string.yse, null)
                    .setMessage("未知错误")
                    .create();

            @Override
            protected Bitmap doInBackground(Void... params) {
                String invitNum = null;
                try {
                    invitNum = HttpTool.sendOkHttpRequest(CreateScanCode.this, HttpTool.CODE_A_INVITE_NUM, oid);
                } catch (ConnectException e) {
                    e.printStackTrace();
                    alertDialog.setMessage("网络错误");
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    alertDialog.setMessage("未知错误");
                    return null;
                }

                InvitInfo invitInfo = new InvitInfo(((MyApp) CreateScanCode.this.getApplication()).getUid(), oid, invitNum);
                return QRCodeEncoder.syncEncodeQRCode(new Gson().toJson(invitInfo), 450);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap == null) {
                    alertDialog.show();
                } else {
                    alertDialog = null;
                    imgSQ.setImageBitmap(bitmap);
                }
                progressBar.setVisibility(View.INVISIBLE);
                imgToRefresh.setVisibility(View.VISIBLE);

            }
        }.execute();
    }
}
